﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace ClientLibrary
{
    public class XML
    {
        public string xml_filename { get; set; }
        public string directory { get; set; }

        public XML(string xml_filename, string directory)
        {
            this.xml_filename = xml_filename;
        }

        public void WriteXML_Settings(string firstrun, string mysql_username, string mysql_password, string mysql_database, string mysql_host, string xml_export_path, string ghostscript_path, string batchcount)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            
            try
            {
                XmlWriter xmlWriter = XmlWriter.Create(this.directory+this.xml_filename, settings);
                xmlWriter.WriteStartDocument();
                xmlWriter.WriteStartElement("settings");

                // FIRST RUN OPTION
                xmlWriter.WriteStartElement("firstrun");
                xmlWriter.WriteAttributeString("value", firstrun);
                //xmlWriter.WriteString("yes");
                xmlWriter.WriteEndElement();            
                // MYSQL EXPORT OPTION
                xmlWriter.WriteStartElement("mysql_export");
                // MYSQL EXPORT PARAMETERS
                xmlWriter.WriteAttributeString("value", mysql_username);
                xmlWriter.WriteString("db_username");      
                xmlWriter.WriteEndElement();
                xmlWriter.WriteStartElement("mysql_export");
                xmlWriter.WriteAttributeString("value", mysql_password);
                xmlWriter.WriteString("db_password");      
                xmlWriter.WriteEndElement();
                xmlWriter.WriteStartElement("mysql_export");
                xmlWriter.WriteAttributeString("value", mysql_database);
                xmlWriter.WriteString("db_name");
                xmlWriter.WriteEndElement();
                xmlWriter.WriteStartElement("mysql_export");
                xmlWriter.WriteAttributeString("value", mysql_host);
                xmlWriter.WriteString("localhost");
                xmlWriter.WriteEndElement();
                // XML EXPORT PARAMETERS
                xmlWriter.WriteStartElement("xml_export");
                xmlWriter.WriteAttributeString("value", xml_export_path);
                xmlWriter.WriteString("default_path");
                xmlWriter.WriteEndElement();
                // GHOST SCRIPT PATH
                xmlWriter.WriteStartElement("ghostscript_path");
                xmlWriter.WriteAttributeString("value", ghostscript_path);
                xmlWriter.WriteString("scriptpath");
                xmlWriter.WriteEndElement();
                // BATCH COUNTER
                xmlWriter.WriteStartElement("batch_counter");
                xmlWriter.WriteAttributeString("value", batchcount);            
                xmlWriter.WriteEndElement();
            
                // END OF FILE
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndDocument();            
                xmlWriter.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void ReadXML_Settings(SystemState ss)
        {
            StringBuilder output = new StringBuilder();
            using (XmlReader reader = XmlReader.Create(new StreamReader(this.directory+this.xml_filename)))
            {
                reader.ReadToFollowing("firstrun");
                reader.MoveToFirstAttribute();
                if (reader.Value == "yes")
                {
                    ss.FirstRun = true;
                }
                else
                {
                    ss.FirstRun = false;
                }
                //Console.WriteLine("Run settings: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("mysql_export");
                reader.MoveToFirstAttribute();
                ss.Mysql_user = reader.Value;
                //Console.WriteLine("Mysql export: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("mysql_export");
                reader.MoveToFirstAttribute();
                ss.Mysql_password = reader.Value;
                //Console.WriteLine("Mysql export: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("mysql_export");
                reader.MoveToFirstAttribute();
                ss.Mysql_database = reader.Value;
                //Console.WriteLine("Mysql export: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("mysql_export");
                reader.MoveToFirstAttribute();
                ss.Mysql_host = reader.Value;
                //Console.WriteLine("Mysql export: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("xml_export");
                reader.MoveToFirstAttribute();
                ss.Xml_path = reader.Value;
                //Console.WriteLine("XML export: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("ghostscript_path");
                reader.MoveToFirstAttribute();
                ss.Ghostscript_path = reader.Value;
                //Console.WriteLine("Ghostscript path: " + reader.Value + " " + reader.ReadString());

                reader.ReadToFollowing("batch_counter");
                reader.MoveToFirstAttribute();
                ss.LastBatch = Int32.Parse(reader.Value);
                //Console.WriteLine("Batch counter: " + reader.Value + " " + reader.ReadString());
                reader.Close();
            }

        }

        public void WriteXML_Templates(SystemState ss)
        {
            List<Template> templates = ss.getTemplates();
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            try
            {
                XmlWriter xmlWriter = XmlWriter.Create(this.directory + this.xml_filename, settings);
                xmlWriter.WriteStartDocument();
                xmlWriter.WriteStartElement("templates");

                for (int i = 0; i < templates.Count; i++)
                {
                    xmlWriter.WriteStartElement("template");
                    xmlWriter.WriteAttributeString("name", templates[i].Name);
                    
                    for (int j = 0; j < templates[i].getFields().Count; j++)
                    {
                        xmlWriter.WriteStartElement("field");
                        xmlWriter.WriteAttributeString("name", templates[i].getFields()[j].Name);
                        xmlWriter.WriteAttributeString("x", templates[i].getFields()[j].X.ToString());
                        xmlWriter.WriteAttributeString("y", templates[i].getFields()[j].Y.ToString());
                        xmlWriter.WriteAttributeString("width", templates[i].getFields()[j].Width.ToString());
                        xmlWriter.WriteAttributeString("height", templates[i].getFields()[j].Height.ToString());
                        xmlWriter.WriteEndElement();
                    }
                    xmlWriter.WriteEndElement();
                }
                                
                // END OF FILE
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndDocument();
                xmlWriter.Close();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public void ReadXML_Templates(SystemState ss)
        {
            StringBuilder output = new StringBuilder();
            
            using (XmlReader reader = XmlReader.Create(new StreamReader(this.directory + this.xml_filename)))
            {
                try
                {
                    int index = -1;
                    while (reader.Read())
                    {
                        Template template = new Template();
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "template"))
                        {
                            if (reader.HasAttributes)
                            {
                                //Console.WriteLine("template name: "+reader.GetAttribute("name"));
                                template.Name = reader.GetAttribute("name");
                                ss.getTemplates().Add(template);
                                index++;
                            }
                        }
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "field"))
                        {
                            if (reader.HasAttributes)
                            {
                                //Console.WriteLine("field name: " + reader.GetAttribute("name"));
                                Field field = new Field();
                                field.Name = reader.GetAttribute("name");
                                field.X = Int32.Parse(reader.GetAttribute("x"));
                                field.Y = Int32.Parse(reader.GetAttribute("y"));
                                field.Width = Int32.Parse(reader.GetAttribute("width"));
                                field.Height = Int32.Parse(reader.GetAttribute("height"));
                                
                                ss.getTemplates()[index].getFields().Add(field);
                            }
                        }

                    }
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            
            }
        }
    }
}
